﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Jungle.Code.DAL;

namespace Jungle.Prodamo.PriceManager
{
    public class Producer
    {
        private int producerId;
        private string title;
        private string webSite;
        private string logo;

        public int ProducerId { get { return producerId; } set { producerId = value; } }
        public string Title { get { return title; } set { title = value; } }
        public string WebSite { get { return webSite; } set { webSite = value; } }
        public string Logo { get { return logo; } set { logo = value; } }

        public void Add()
        { 
            using(SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Producer_Add", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@Title", SqlDbType.NChar, 256).Value = title;
                command.Parameters.Add("@WebSite", SqlDbType.NChar, 256).Value = webSite;
                command.Parameters.Add("@Logo", SqlDbType.Text).Value = logo;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                if(reader.Read())
                {
                    producerId = Convert.ToInt32(reader.GetValue(0));
                }
                else producerId = -1;
                command.Connection.Close();
            }
        }
        public void Delete()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Producer_Delete", connect);
                command.Parameters.Add("@ProducerId", SqlDbType.Int).Value = producerId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public void GetById()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Producer_GetById", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@ProducerId", SqlDbType.Int).Value = producerId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    producerId = (int)reader["ProducerId"];
                    title = reader["Title"].ToString().Trim();
                    webSite = reader["WebSite"].ToString().Trim();
                    logo = reader["Logo"].ToString().Trim();
                }
                reader.Close();
                command.Connection.Close();
            }
        }
        public void GetByTitle()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Producer_GetByTitle", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@Title", SqlDbType.NChar, 256).Value = title;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    producerId = (int)reader["ProducerId"];
                    title = reader["Title"].ToString().Trim();
                    webSite = reader["WebSite"].ToString().Trim();
                    logo = reader["Logo"].ToString().Trim();
                }
                reader.Close();
                command.Connection.Close();
            }
        }


    }
}